﻿function loadimage(){
	document.getElementById("randImage").src="/imageCode.jsp?"+Math.random();
}
function submitData(blogId){
	var content=$("#content").val();
	var imageCode=$("#imageCode").val();
	var qqNumber = $("input[name='qqNumber']").val() ;
	if (!RegExp(/^[1-9][0-9]{4,9}$/).test($.trim(qqNumber))) {  
		layer.msg('请输入正确的qq号码', {icon: 2});
	}else if(content==null || content==""){
		layer.msg('请输入评论内容', {icon: 2});
	}else if(imageCode==null || imageCode==""){
		layer.msg('请输入验证码', {icon: 2});
	}else{
		$.post("/comment/" + blogId + "/addInJSON.html",{"content":content,'imageCode':imageCode,'qqNumber':qqNumber}
		,function(result){
			if(result == "true"){
				layer.msg("评论成功，等待管理员审核", {icon: 1});
				$("#content").val("") ;
				$("#imageCode").val("");
				$("input[name='qqNumber']").val("") ;
			}else{
				layer.msg("验证码输入错误，请从新输入", {icon: 2});
			}
		}
		);
	}
}

function filp(pageNum,blogId){
	var pageBean ;
	$.get("/comment/" + pageNum + "/" + blogId + "/listInJSON.html",{},function(data){
		pageBean = JSON.parse(data) ;
		$.each(pageBean.data,function(index,content){
			var comment = "" ;
			if(index != 0){	
				comment = $(".commentDatas").html() ;
			}

			$(".commentDatas").html(comment + '<div class="comment"><span><font>' + content.floor + '楼&nbsp;&nbsp;&nbsp;&nbsp;' + content.qqNumber + '：</font>' + content.content + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[&nbsp;' + content.commentTime + '&nbsp;]</span></div>') ;
		})
		var comment = $(".commentDatas").html() ;
		var page = '<nav>' ;
		page += '<ul class="pagination pagination-sm">' ;
		page += '<li><a href="javascript:void(0)" onclick="filp(1,' + blogId + ')">首页</a></li>' ;

		if(pageNum == 1){
			page += '<li class="disabled"><a href="javascript:void(0)">上一页</a></li>' ;
		}else{
			page += '<li><a href="javascript:void(0)" onclick="filp(' + (pageNum - 1) + ',' + blogId + ')">上一页</a></li>' ;
		}
		for(var i=pageBean.pageNumber-2;i<=pageBean.pageNumber+2;i++){
			if(i < 1){
				continue ;
			}
			if(i > pageBean.totalPage){
				break ;
			}
			if(i == pageNum){
				page += '<li class="active"><a href="javascript:void(0)">' + i + '</a></li>'
			}else{
				page += '<li><a onclick="filp(' + i + ',' + blogId + ')" href="javascript:void(0)">' + i + '</a></li>'
			}
		}
		if(pageBean.totalPage == pageNum){
			page += '<li class="disabled"><a href="javascript:void(0)">下一页</a></li>' ;
		}else{
			page += '<li><a href="javascript:void(0)" onclick="filp(' + (pageNum + 1) + ',' + blogId + ')">下一页</a></li>' ;
		}
		page += '<li><a href="javascript:void(0)" onclick="filp(' + pageBean.totalPage + ',' + blogId + ')">尾页</a></li>' ;
		page += '</ul>'
			page += '<nav>' ;
		$(".commentDatas").html(comment + page) ;
	}) ;
}